package com.han.hotelplat.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter;

import com.alibaba.fastjson.JSONObject;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.utils.RequestUtil;
import com.han.hotelplat.pojo.systemuser.Systemuser;


public class URLPermissionsFilter extends PermissionsAuthorizationFilter{
	/**
	 *@param mappedValue 指的是在声明url时指定的权限字符串，如/User/create.do=perms[User:create].我们要动态产生这个权限字符串，所以这个配置对我们没用
	 */
	@Override
	public boolean isAccessAllowed(ServletRequest request,
			ServletResponse response, Object mappedValue) throws IOException {
		HttpServletRequest req = (HttpServletRequest) request;
		String url = req.getRequestURL().toString();
		
		boolean re = super.isAccessAllowed(request, response, buildPermissions(request,mappedValue));
		if(url.contains("exp")){
			RequestUtil.setSessionAttribute("isExport", re);
			System.out.println(url);
			return true;
		}
		return re;
	}
	/**
	 * 根据请求URL产生权限字符串，这里只产生，而比对的事交给Realm
	 * @param request
	 * @return
	 */
	protected String[] buildPermissions(ServletRequest request,Object mappedValue) {
		
		List<String> permissions = new ArrayList<String>();
		
		//HttpServletRequest req = (HttpServletRequest) request;
		
		String[] mv = (String[])mappedValue;
		for(String s:mv){
			permissions.add(s);
		}
		/*if(req.getParameter("sele.gameId")!=null){
			permissions.add("resource:sele.gameId:"+req.getParameter("sele.gameId"));
		}*/
		String[] perms = new String[permissions.size()];
		for(int i=0;i<permissions.size();i++){
			perms[i]=permissions.get(i);
		}
		for(int i=0;i<permissions.size();i++){
			System.out.println("premes："+perms[i]);
		}
		return perms;
	}
	
	/**
	 * 是否是Ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjax(ServletRequest request) {
		String header = ((HttpServletRequest) request).getHeader("X-Requested-With");
		if ("XMLHttpRequest".equalsIgnoreCase(header)) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
	
	/**
	 *  使用	response 输出JSON
	 * @param hresponse
	 * @param resultMap
	 * @throws IOException
	 */
	public static void out(ServletResponse response, Map<String, Object> resultMap) {
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");// 设置编码
			response.setContentType("application/json");// 设置返回类型
			out = response.getWriter();
			out.println(JSONObject.toJSONString(resultMap).toString());// 输出
		} catch (Exception e) {
		} finally {
			if (null != out) {
				out.flush();
				out.close();
			}
		}
	}
	/**
	 *  使用	response 输出JSON
	 * @param hresponse
	 * @param resultMap
	 * @throws IOException
	 */
	public static void redirect(HttpServletResponse response, String redirectURL) {
		try {
			   response.setCharacterEncoding("utf-8");
			   response.sendRedirect(redirectURL);
			} catch (IOException e) {
				 e.printStackTrace();
			}
	}
}
